package outoforder.leetcode01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;



/**
 * @author shhjiang_001
 * @create 2020-07-12 19:51
 */
public class Solution969 {
    ArrayList<Integer> res = new ArrayList<>();
    public List<Integer> pancakeSort(int[] A) {
        sortInts(A,A.length);
        return res;
    }

    private void sortInts(int[] cakes, int n) {
        if (n==1) return;
        int maxCake = 0;
        int maxCakeIndex = 0;
        for (int i = 0; i < n; i++) {
            if (cakes[i] > maxCake){
                maxCake = cakes[i];
                maxCakeIndex = i;
            }
        }
        // 第一次翻转，将最大的饼放在最上面
        if (maxCakeIndex!=0){
            reverseInts(cakes,0,maxCakeIndex);
            res.add(maxCakeIndex+1);
        }
        // 第二次翻转，将最大的饼放在最下面
        reverseInts(cakes,0,n-1);
        res.add(n);
        sortInts(cakes,n-1);
    }

    private void reverseInts(int[] cakes, int i, int j) {
        while (i < j){
            int temp = cakes[i];
            cakes[i] = cakes[j];
            cakes[j] = temp;
            i++;
            j--;
        }
    }
    @Test
    public void test(){
        pancakeSort(new int[]{3,2,4,1});
    }
}
